package com.sz.app.oss.api.system;

import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.BizException;
import com.sz.common.core.system.entity.OperationLog;
import com.sz.common.core.system.service.OperationLoggerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Function: OperationLogsController <br>
 * Author: Charles <br>
 * Date: 2016-11-22 16:31:00
 */
@RestController
@RequestMapping("${api}/sys/logs")
@Api(description = " ", tags = "007、操作日志")
public class OperationLogsController {
    @Autowired
    private OperationLoggerService operationLoggerService;

    /**
     * @param userId        --用户Id
     * @param domainId      --域Id(1/2/3)
     * @param moduleName    --模块名称
     * @param subModuleName --子模块名称
     * @param action        --动作
     * @param key           --搜索关键字
     * @param ip            --用户Ip
     * @param level         --级别
     * @param desc          --描述
     * @param startTime     --起始时间
     * @param endTime       --终止时间
     * @param pageIndex     --页面索引(从1开始)
     * @param pageSize      --页面大小
     * @return
     * @throws BizException
     */
    @ApiOperation(value = "搜索日志", notes = "搜索日志，返回日志列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户Id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "domainid", value = "域Id(1/2/3)", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "moduleName", value = "模块名称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "subModuleName", value = "子模块名称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "action", value = "action", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "key", value = "搜索关键字", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ip", value = "ip", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "级别(1/2)", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "desc", value = "附加信息", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "起始时间(yyyy-MM-dd HH:mm:ss)", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间(yyyy-MM-dd HH:mm:ss)", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页 默认第一页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pagesize", value = "页面大小", required = false, dataType = "int", paramType = "query")})
    @RequestMapping(value = "/search", method = {RequestMethod.GET})
    public ResultDto search(@RequestParam(name = "userid", required = false, defaultValue = "0") int userId,
                            @RequestParam(name = "domainid", required = false, defaultValue = "0") int domainId,
                            @RequestParam(required = false, defaultValue = "") String moduleName,
                            @RequestParam(required = false, defaultValue = "") String subModuleName,
                            @RequestParam(required = false, defaultValue = "") String action,
                            @RequestParam(required = false, defaultValue = "") String key,
                            @RequestParam(required = false, defaultValue = "") String ip,
                            @RequestParam(required = false, defaultValue = "0") int level,
                            @RequestParam(required = false, defaultValue = "") String desc,
                            @RequestParam(required = false, defaultValue = "") String startTime,
                            @RequestParam(required = false, defaultValue = "") String endTime,
                            @RequestParam(required = false, defaultValue = "1") int pageIndex,
                            @RequestParam(name = "pagesize", required = false, defaultValue = "10") int pageSize) throws BizException {
        Date startDate = null;
        Date endDate = null;
        if (startTime != null && startTime.trim().length() > 0) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                startDate = format.parse(startTime);
            } catch (Exception ex) {
            }
        }
        if (endTime != null && endTime.trim().length() > 0) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                endDate = format.parse(endTime);
            } catch (Exception ex) {
            }
        }
        return operationLoggerService.findByFilterListPage(userId, domainId, moduleName, subModuleName,
                action, key, ip, level, desc, startDate, endDate, pageIndex, pageSize);
    }

    /**
     * 获取单个日志
     */
    @ApiOperation(value = "根据id获取日志", notes = "根据id获取单个日志")
    @ApiImplicitParam(name = "id", value = "日志的id ", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getLogById(@PathVariable long id) {
        OperationLog log = operationLoggerService.getOperationLog(id);
        ResultDto resultDto = new ResultDto(log);
        return resultDto;
    }

    /**
     * 删除单个日志
     */
    @ApiOperation(value = "根据id删除日志", notes = "删除单个日志")
    @ApiImplicitParam(name = "id", value = "日志的id ", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
    public ResultDto deleteLogById(@PathVariable long id) {
        List<Long> list = new ArrayList<>();
        list.add(id);
        operationLoggerService.deleteByIds(list);
        ResultDto resultDto = new ResultDto(id);
        return resultDto;
    }

    /**
     * 批量删除日志
     */
    @ApiOperation(value = "批量删除日志", notes = "批量删除日志")
    @ApiImplicitParam(name = "logIds", value = "日志的id列表 ", required = true, dataType = "Array[long]", paramType = "body")
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    public ResultDto deleteLogByIds(@RequestBody List<Long> logIds) {
        if (logIds != null && logIds.size() > 0) {
            operationLoggerService.deleteByIds(logIds);
        }
        ResultDto resultDto = new ResultDto();
        return resultDto;
    }
}
